﻿/*-------------------------------------------------------------------------
* 命名空间: IEventAggregatorSimple.ViewModels/MainViewModel
* 类       名: MainViewModel
* 功      能:  TD
* 时      间:  2023/12/21 17:48:50
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace IEventAggregatorSimple.ViewModels
{
    public class MainViewModel : BindableBase
    {
        private readonly IEventAggregator eventAggregator;

        public MainViewModel(IEventAggregator eventAggregator)
        {
            this.eventAggregator = eventAggregator;

            SubscribeEventCommand = new DelegateCommand(DoSubscribeEvent);
            PublishEventCommand = new DelegateCommand(DoPublishEvent);
        }

        // 发布消息
        private void DoPublishEvent()
        {
            eventAggregator.GetEvent<MainVmEvent>().Publish("Hello,Prism!");
        }

        // 订阅事件
        private void DoSubscribeEvent()
        {
            eventAggregator.GetEvent<MainVmEvent>().Subscribe(OnEventReceived);
        }

        // 订阅的事件发生的时候,要调用的方法
        private void OnEventReceived(string message)
        {
            MessageBox.Show($"接收到发布者发布的消息为: {message}", "订阅事件发生提示");
        }

        public DelegateCommand SubscribeEventCommand { get; private set; }


        // 发布事件,其实就是产生一个事件,它的订阅者会收到这个消息.
        public DelegateCommand PublishEventCommand { get; private set; }

    }


    public class MainVmEvent : PubSubEvent<string>
    {

    }
}
